package com.fdb.efp.esb.service.impl.basicfn;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.efp.esb.service.facade.basicfn.MonReliefDatasAppService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.basicfn.MonReliefDatasAppReqBean;
import com.fdb.efp.esb.service.bo.resp.basicfn.MonReliefDatasAppRespBean;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import com.fdb.efp.esb.service.client.impl.Lhx2ESBBeanCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 贷款贷后账务处理贷款豁免接口实现
 */
@Service("monReliefDatasAppService")
public class MonReliefDatasAppServiceImpl implements MonReliefDatasAppService {

    private static Logger logger = LoggerFactory.getLogger(MonReliefDatasAppServiceImpl.class);

    @Override
    public MonReliefDatasAppRespBean applyMonReliefDatasApp(MonReliefDatasAppReqBean monReliefDatasAppReqBean) throws ESBException {
        String msg = new StringBuffer("")
                .append(monReliefDatasAppReqBean.getAcctNo())
                .append("_")
                .append(monReliefDatasAppReqBean.getAcctNoSrlNo())
                .append("_")
                .append(monReliefDatasAppReqBean.getRcylAmt())
                .toString();
        logger.info("======>调用贷款贷后账务处理贷款豁免接口【30210004_02】开始<======" +msg);
        MonReliefDatasAppRespBean monReliefDatasAppRespBean = null;
        try {
            EsbRespServiceBean recvBean = ESBClientFactory.buildClient()
                    .withBody(monReliefDatasAppReqBean)
                    .withTradeNo(ESBConstant.TRADE_NO_30210004)
                    .withScene(ESBConstant.SECNE_NO_02)
                    .withESBBeanFactory(Lhx2ESBBeanCreator.getINSTANCE())
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                monReliefDatasAppRespBean = recvBean.getBody(MonReliefDatasAppRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用贷款贷后账务处理贷款豁免接口【30210004_02】结束，返回信息：" + JSONObject.toJSONString(monReliefDatasAppRespBean));
        } catch (Exception e) {
            logger.error("调用贷款贷后账务处理贷款豁免接口【30210004_02】出现异常：" + e.getMessage());
            throw new ESBException("调用贷款贷后账务处理贷款豁免接口【30210004_02】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用贷款贷后账务处理贷款豁免接口【30210004_02】结束<======"+msg);
        }
        return monReliefDatasAppRespBean;
    }
}